rm(list = setdiff(ls(), lsf.str()))
#load data
wave3<- haven::read_stata("Study 1/Original Data/c_indresp.dta")
wave3 <- wave3[,which(colnames(wave3)%in%c("pidp","c_sex",  "c_scptrt5o1",  "c_scptrt5o2",  "c_scptrt5o3",  "c_scptrt5c1",  "c_scptrt5c2", "c_scptrt5c3",  "c_scptrt5e1", "c_scptrt5e2",  "c_scptrt5e3",  "c_scptrt5a1",  "c_scptrt5a2",  "c_scptrt5a3", "c_scptrt5n1",  "c_scptrt5n2",  "c_scptrt5n3"))]

wave6<- haven::read_stata("Study 1/Original Data/f_indresp.dta")
wave6 <- wave6[,which(colnames(wave6)%in%c("pidp", "f_gor_dv", "f_vote4", "f_vote3", "f_poleff3", "f_poleff4", "f_hiqual_dv", "f_dvage", "f_fimngrs_dv"))]

#merge data
data_UK <- merge(wave3, wave6,by="pidp")

###Restrict data to England-------------
data_UK$England<-ifelse(data_UK$f_gor_dv==-9 | data_UK$f_gor_dv==10 | data_UK$f_gor_dv==11 | data_UK$f_gor_dv==12, 0,1)
data_UK<-subset(data_UK, England==1)

###Vote for populist-----------------
#Vote for populist
data_UK$attach_populist<-ifelse(data_UK$f_vote4==12,1,
                              ifelse(data_UK$f_vote4< -3| data_UK$f_vote4==96, NA,0))
#Attach to populist
data_UK$vote_populist<-ifelse(data_UK$f_vote3==12,1,
                              ifelse(data_UK$f_vote3< -1 | data_UK$f_vote3==96, NA,0))

#Total populist
data_UK$total_populist<-ifelse(is.na(data_UK$attach_populist), data_UK$vote_populist, data_UK$attach_populist)

###Cynicsim-----------------
data_UK$w6_poleff3<-car::recode(data_UK$f_poleff3, "-10=NA; -9=NA; -7=NA; -2=NA; -1=NA")
data_UK$w6_poleff4<-car::recode(data_UK$f_poleff4, "-10=NA; -9=NA; -7=NA; -2=NA; -1=NA")

data_UK$w6_poleff3<-6-data_UK$w6_poleff3
data_UK$w6_poleff4<-6-data_UK$w6_poleff4

data_UK$w6_cynicism<- rowMeans(with(data_UK, data.frame(w6_poleff3, w6_poleff4), na.rm=T))

###Education------------
data_UK$Ed_degree <- ifelse(data_UK$f_hiqual_dv==1,1,0)
data_UK$Ed_higherdegree <- ifelse(data_UK$f_hiqual_dv==2,1,0)
data_UK$Ed_Alevel <- ifelse(data_UK$f_hiqual_dv==3,1,0)
data_UK$Ed_GSCElevel <- ifelse(data_UK$f_hiqual_dv==4,1,0)
data_UK$Ed_OtherQualification <- ifelse(data_UK$f_hiqual_dv==5,1,0)
data_UK$Ed_Missing <- ifelse(data_UK$f_hiqual_dv==-9| data_UK$f_hiqual_dv==-8,1,0)

###Sex--------
data_UK$female<-ifelse(data_UK$c_sex==2, 1,0)

###Age: 18+------------
data_UK$age<-car::recode(data_UK$f_dvage, "-2=NA; -1=NA; 16=NA; 17=NA")

###Income--------------
data_UK$income<-ifelse(data_UK$f_fimngrs_dv<0, NA, data_UK$f_fimngrs_dv)
data_UK$income<-zero1(data_UK$income)

###Openenss to Experience------------
data_UK$o1<-ifelse(data_UK$c_scptrt5o1<1, NA, data_UK$c_scptrt5o1)
data_UK$o2<-ifelse(data_UK$c_scptrt5o2<1, NA, data_UK$c_scptrt5o2)
data_UK$o3<-ifelse(data_UK$c_scptrt5o3<1, NA, data_UK$c_scptrt5o3)
#psych::alpha(with(data_UK, data.frame(o1, o2, o3)))
data_UK$w3_open<- rowMeans(with(data_UK, data.frame(o1, o2, o3), na.rm=T))

###Conscientiousness------------
data_UK$c1<-ifelse(data_UK$c_scptrt5c1<1, NA, data_UK$c_scptrt5c1)
data_UK$c2<-8-ifelse(data_UK$c_scptrt5c2<1, NA, data_UK$c_scptrt5c2)
data_UK$c3<-ifelse(data_UK$c_scptrt5c3<1, NA, data_UK$c_scptrt5c3)
##psych::alpha(with(data_UK, data.frame(c1, c2, c3)))
data_UK$w3_con<- rowMeans(with(data_UK, data.frame(c1, c2, c3), na.rm=T))

###Extraversion------------------
data_UK$e1<-ifelse(data_UK$c_scptrt5e1<1, NA, data_UK$c_scptrt5e1)
data_UK$e2<-ifelse(data_UK$c_scptrt5e2<1, NA, data_UK$c_scptrt5e2)
data_UK$e3<-8-ifelse(data_UK$c_scptrt5e3<1, NA, data_UK$c_scptrt5e3)
#psych::alpha(with(data_UK, data.frame(e1, e2, e3)))
data_UK$w3_ext<- rowMeans(with(data_UK, data.frame(e1, e2, e3), na.rm=T))

###Agreeableness-----------
data_UK$a1<-8-ifelse(data_UK$c_scptrt5a1<1, NA, data_UK$c_scptrt5a1)
data_UK$a2<-ifelse(data_UK$c_scptrt5a2<1, NA, data_UK$c_scptrt5a2)
data_UK$a3<-ifelse(data_UK$c_scptrt5a3<1, NA, data_UK$c_scptrt5a3)
#psych::alpha(with(data_UK, data.frame(a1, a2, a3)))
data_UK$w3_agre<- rowMeans(with(data_UK, data.frame(a1, a2, a3), na.rm=T))

###Neurotcism---------------
data_UK$n1<-ifelse(data_UK$c_scptrt5n1<1, NA, data_UK$c_scptrt5n1)
data_UK$n2<-ifelse(data_UK$c_scptrt5n2<1, NA, data_UK$c_scptrt5n2)
data_UK$n3<-8-ifelse(data_UK$c_scptrt5n3<1, NA, data_UK$c_scptrt5n3)
#psych::alpha(with(data_UK, data.frame(n1, n2, n3)))
data_UK$w3_neu<- rowMeans(with(data_UK, data.frame(n1, n2, n3), na.rm=T))

save(data_UK, file="Study 1/Altered Data/Study1_UK_understanding.RData")
